<template>
  <div class="app-container">
    <div class="filter-container clearfix mt-15">
      <!-- <el-select v-model="listQuery.srid" class="filter-item" placeholder="选择大区" clearable multiple width="300">
        <el-option
          v-for="item in serverRegionList"
          :key="item.id"
          :label="item.name + ' (' + item.id + ')'"
          :value="item.id"
        />
      </el-select>
      <searchBar :show-keyword="false" :show-channel-id="false" :show-server-id="false" @transferFilter="handleFilter" /> -->
      <el-button v-if="checkPermission(permissionRoles.channel_config)" class="filter-item" type="primary" icon="el-icon-plus" @click="handleAdd">配置表目录</el-button>
    </div>
    <div class="filter-container" style="margin-bottom: 10px;">
      <el-checkbox-group v-model="listQuery.srid" @change="handleFilter">
        <el-checkbox v-for="(item, index) in serverRegionList" :key="index" :value="item.id" :label="item.id"> {{ item.name + ' (' + item.id + ')' }}</el-checkbox>
      </el-checkbox-group>
    </div>
    <el-table v-loading="listLoading" :data="list" border height="725">
      <el-table-column fixed prop="cid" align="center" min-width="80" label="渠道ID"></el-table-column>
      <el-table-column fixed prop="srname" align="center" min-width="150" label="大区"></el-table-column>
      <el-table-column show-overflow-tooltip prop="config.config" align="center" min-width="300" label="配置表目录"></el-table-column>
      <el-table-column show-overflow-tooltip prop="hotupdate_path" align="center" min-width="300" label="热更目录"></el-table-column>
      <el-table-column prop="version" align="center" min-width="80" label="版本"></el-table-column>
      <el-table-column prop="config_updated_at" align="center" min-width="120" label="更新时间"></el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button v-if="checkPermission(permissionRoles.channel_config_edit)" type="text" size="small" @click="handleEdit(scope)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :visible.sync="dialogVisible" :title="textMap[dialogType]" :close-on-click-modal="false">
      <el-form ref="dataForm" :model="dataForm" :rules="rules" label-width="100px" label-position="left">
        <el-form-item label="渠道ID" prop="id">
          <el-select v-model="dataForm.id" :disabled="cidDisabled" filterable clearable class="defaut-input" @change="handleChannelConfig()">
            <el-option
              v-for="item in lists"
              :key="item.id"
              :label="item.cid"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="目录" prop="config">
          <el-input v-model="dataForm.config.config" class="defaut-input" placeholder="填写目录" />
        </el-form-item>
        <el-form-item label="热更目录" prop="config">
          <el-input v-model="dataForm.hotupdate_path" class="defaut-input" placeholder="热更目录" />
        </el-form-item>
        <el-form-item label="版本" prop="version">
          <!-- <el-input v-model="dataForm.version" class="defaut-input" placeholder="版本" /> -->
          <el-select v-model="dataForm.version" filterable clearable class="defaut-input">
            <el-option
              v-for="item in versionList"
              :key="item"
              :label="item"
              :value="item"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div class="tr">
        <el-button type="danger" @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="confirmSubmit()">确定</el-button>
      </div>
    </el-dialog>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>

<script>
import checkPermission from '@/utils/permission' // 权限判断函数
import { objectMerge, deepClone } from '@/utils'
import { getChannelList, handleChannel, getServerRegionList, getVersionList, getAllChannel, getSDKList } from '@/api/ops'
import Pagination from '@/components/Pagination'
import SearchBar from '@/components/SearchBar'
import { version } from 'nprogress'

const defaultItem = {
  id: '',
  config: { config: '' },
  hotupdate_path: '',
  version: ''
}
export default {
  name: 'ChannelConfig',
  components: { SearchBar, Pagination },
  data() {
    return {
      permissionRoles: this.$store.state.user.permissionRoles,
      total: 0,
      listQuery: {
        page: 1,
        limit: 100,
        srid: [],
      },
      searchBarQuery: {},
      listLoading: false,
      dataForm: Object.assign({}, defaultItem),
      rules: {
        cid: [{ required: true, message: '请填写渠道ID', trigger: 'blur' }],
        name: [{ required: true, message: '请填写渠道名', trigger: 'blur' }],
        srid: [{ required: true, message: '请选择至少一个大区', trigger: 'blur' }]
      },
      list: [],
      lists: [],
      listId: {},
      serverRegionList: [],
      channelList: [],
      sdkList: [],
      versionList: [],
      dialogVisible: false,
      cidDisabled: false,
      dialogType: 'new',
      textMap: {
        edit: '编辑',
        new: '新增'
      }
    }
  },
  created() {
    this.init()
    this.getServerRegionList()
    this.getVersionList()
  },
  methods: {
    checkPermission,
    init() {
      this.getList()
      this.getAllChannel()
      // this.getSDKList()
    },
    async getServerRegionList() {
      const res = await getServerRegionList()
      this.serverRegionList = res.data
      this.listQuery.srid = this.serverRegionList.map(item => item.id)
    },
    async getVersionList() {
      const res = await getVersionList()
      this.versionList = res.data
    },
    async getSDKList() {
      const res = await getSDKList()
      this.sdkList = res.data
    },
    handleChannelConfig() {
      const config = this.listId[this.dataForm.id].config
      console.log(config.config)
      this.dataForm.config.config = config.config
    },
    handleFilter(query) {
      this.searchBarQuery = query
      this.getList()
    },
    async getList() {
      const query = objectMerge(this.searchBarQuery, this.listQuery)
      this.listLoading = true
      const res = await getChannelList(Object.assign({}, query, { }))
      this.listLoading = false
      this.list = []
      for (const key in res.data.items) {
        if (res.data.items[key].config) {
          if (res.data.items[key].config.config) {
            this.list.push(res.data.items[key])
          }
        }
        this.listId[res.data.items[key].id] = res.data.items[key]
      }
      this.lists = res.data.items
      this.total = res.data.total
    },
    async getAllChannel() {
      const res = await getAllChannel()
      this.channelList = res.data
    },
    handleAdd() {
      this.dataForm = Object.assign({}, defaultItem)
      this.dialogType = 'new'
      this.dialogVisible = true
      this.cidDisabled = false
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleEdit(scope) {
      this.dialogType = 'edit'
      this.dialogVisible = true
      this.cidDisabled = true
      this.dataForm = deepClone(scope.row)
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleCid() {
      if (this.dataForm.login_cid === '') this.dataForm.login_cid = this.dataForm.cid
    },
    confirmSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.dialogType === 'edit'
          handleChannel(this.dataForm).then(response => {
            if (response.status) {
              this.getList()
              this.dialogVisible = false
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
            } else {
              this.message.error(response.message)
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.el-table {
  font-size: 14px;
}
.el-table td, .el-table th {
  padding: 10px 0 !important;
}
.el-table th>.cell {
  padding-left: 10px;
  padding-right: 10px;
}

.el-table td {
  text-align: left !important;
}
</style>
